home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_09 / single2 / cmostr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-07  |  9.8 KB  |  265 lines

  1. //    cmostr.h
  2. //    modification of Microsoft Foundation Class code supporting CString, to make use
  3. //    of CMemoryObject base class.
  4.  
  5. // Microsoft Foundation Classes C++ library.
  6. // Copyright (C) 1992 Microsoft Corporation,
  7. // All rights reserved.
  8.  
  9. // This source code is only intended as a supplement to the
  10. // Microsoft Foundation Classes Reference and Microsoft
  11. // QuickHelp and/or WinHelp documentation provided with the library.
  12. // See these sources for detailed information regarding the
  13. // Microsoft Foundation Classes product.
  14.  
  15. #ifndef _CMOSTR_H_
  16. #define _CMOSTR_H_
  17.  
  18. #ifndef __cplusplus
  19. #error Microsoft Foundation Classes require C++ compilation (use a .cpp suffix)
  20. #endif
  21.  
  22. #include    <iostream.h>
  23. #include    <string.h>
  24.  
  25. #if !defined (_MOBJECT_H_)
  26.     #include    "mobject.h"
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Strings
  31.  
  32. typedef const char FAR * lp_cc;        //    To fix compiler bug in declaring
  33.                                     //    conversion functions, as advised
  34.                                     //    by MSKB article Q103789
  35.  
  36. class FAR CMOString : public MemoryObject
  37. {
  38. public:
  39.  
  40. // Constructors
  41.     CMOString();
  42.     CMOString(const CMOString& stringSrc);
  43.     CMOString(char ch, int nRepeat = 1);
  44.     CMOString(const char FAR * psz);
  45.     CMOString(const char FAR * pch, int nLength);
  46. #ifdef _NEARDATA
  47.     // Additional versions for far string data
  48. //    CMOString(LPCSTR lpsz);
  49. //    CMOString(LPCSTR lpch, int nLength);
  50. #endif
  51.     ~CMOString();
  52.  
  53. // Attributes & Operations
  54.  
  55.     // as an array of characters
  56.     int GetLength() const;
  57.     BOOL IsEmpty() const;
  58.     void Empty();                       // free up the data
  59.  
  60.     char GetAt(int nIndex) const;       // 0 based
  61.     char operator[](int nIndex) const;  // same as GetAt
  62.     void SetAt(int nIndex, char ch);
  63.     operator lp_cc () const;       // as a C string
  64.  
  65.     // overloaded assignment
  66.     const CMOString& operator=(const CMOString& stringSrc);
  67.     const CMOString& operator=(char ch);
  68.     const CMOString& operator=(const char FAR * psz);
  69.  
  70.     // string concatenation
  71.     const CMOString& operator+=(const CMOString& string);
  72.     const CMOString& operator+=(char ch);
  73.     const CMOString& operator+=(const char FAR * psz);
  74.  
  75.     friend CMOString AFXAPI operator+(const CMOString& string1,
  76.             const CMOString& string2);
  77.     friend CMOString AFXAPI operator+(const CMOString& string, char ch);
  78.     friend CMOString AFXAPI operator+(char ch, const CMOString& string);
  79.     friend CMOString AFXAPI operator+(const CMOString& string, const char FAR * psz);
  80.     friend CMOString AFXAPI operator+(const char FAR * psz, const CMOString& string);
  81.     friend ostream & operator << (ostream & os, const CMOString & str);
  82.     
  83.     // string comparison
  84.     int Compare(const char FAR * psz) const;         // straight character
  85.     int CompareNoCase(const char FAR * psz) const;   // ignore case
  86.     int Collate(const char FAR * psz) const;         // NLS aware
  87.  
  88.     // simple sub-string extraction
  89.     CMOString Mid(int nFirst, int nCount) const;
  90.     CMOString Mid(int nFirst) const;
  91.     CMOString Left(int nCount) const;
  92.     CMOString Right(int nCount) const;
  93.  
  94.     CMOString SpanIncluding(const char FAR * pszCharSet) const;
  95.     CMOString SpanExcluding(const char FAR * pszCharSet) const;
  96.  
  97.     // upper/lower/reverse conversion
  98.     void MakeUpper();
  99.     void MakeLower();
  100.     void MakeReverse();
  101.  
  102.     // searching (return starting index, or -1 if not found)
  103.     // look for a single character match
  104.     int Find(char ch) const;                    // like "C" strchr
  105.     int ReverseFind(char ch) const;
  106.     int FindOneOf(const char FAR * pszCharSet) const;
  107.  
  108.     // look for a specific sub-string
  109.     int Find(const char FAR * pszSub) const;         // like "C" strstr
  110.  
  111.     // input and output
  112. #ifdef _DEBUG
  113.     friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,
  114.                 const CMOString& string);
  115. #endif
  116.     friend CArchive& AFXAPI operator<<(CArchive& ar, const CMOString& string);
  117.     friend CArchive& AFXAPI operator>>(CArchive& ar, CMOString& string);
  118.  
  119.     // Windows support
  120. #ifdef _WINDOWS
  121.     BOOL LoadString(UINT nID);          // load from string resource
  122.                                         // 255 chars max
  123.     // ANSI<->OEM support (convert string in place)
  124.     void AnsiToOem();
  125.     void OemToAnsi();
  126. #endif //_WINDOWS
  127.  
  128.     // Access to string implementation buffer as "C" character array
  129.     char FAR * GetBuffer(int nMinBufLength);
  130.     void ReleaseBuffer(int nNewLength = -1);
  131.     char FAR * GetBufferSetLength(int nNewLength);
  132.  
  133. // Implementation
  134. protected:
  135.     // lengths/sizes in characters
  136.     //  (note: an extra character is always allocated)
  137.     char FAR * m_pchData;       // actual string (zero terminated)
  138.     int m_nDataLength;          // does not include terminating 0
  139.     int m_nAllocLength;         // does not include terminating 0
  140.  
  141.     // implementation helpers
  142.     void Init();
  143.     void AllocCopy(CMOString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  144.     void AllocBuffer(int nLen);
  145.     void AssignCopy(int nSrcLen, const char FAR * pszSrcData);
  146.     void ConcatCopy(int nSrc1Len, const char FAR * pszSrc1Data, int nSrc2Len, const char FAR * pszSrc2Data);
  147.     void ConcatInPlace(int nSrcLen, const char FAR * pszSrcData);
  148. };
  149.  
  150.  
  151. // Compare helpers
  152. BOOL AFXAPI operator==(const CMOString& s1, const CMOString& s2);
  153. BOOL AFXAPI operator==(const CMOString& s1, const char FAR * s2);
  154. BOOL AFXAPI operator==(const char FAR * s1, const CMOString& s2);
  155. BOOL AFXAPI operator!=(const CMOString& s1, const CMOString& s2);
  156. BOOL AFXAPI operator!=(const CMOString& s1, const char FAR * s2);
  157. BOOL AFXAPI operator!=(const char FAR * s1, const CMOString& s2);
  158. BOOL AFXAPI operator<(const CMOString& s1, const CMOString& s2);
  159. BOOL AFXAPI operator<(const CMOString& s1, const char FAR * s2);
  160. BOOL AFXAPI operator<(const char FAR * s1, const CMOString& s2);
  161. BOOL AFXAPI operator>(const CMOString& s1, const CMOString& s2);
  162. BOOL AFXAPI operator>(const CMOString& s1, const char FAR * s2);
  163. BOOL AFXAPI operator>(const char FAR * s1, const CMOString& s2);
  164. BOOL AFXAPI operator<=(const CMOString& s1, const CMOString& s2);
  165. BOOL AFXAPI operator<=(const CMOString& s1, const char FAR * s2);
  166. BOOL AFXAPI operator<=(const char FAR * s1, const CMOString& s2);
  167. BOOL AFXAPI operator>=(const CMOString& s1, const CMOString& s2);
  168. BOOL AFXAPI operator>=(const CMOString& s1, const char FAR * s2);
  169. BOOL AFXAPI operator>=(const char FAR * s1, const CMOString& s2);
  170.  
  171. #define _AFX_INLINE inline
  172.  
  173. _AFX_INLINE int CMOString::GetLength() const
  174.     { return m_nDataLength; }
  175. _AFX_INLINE BOOL CMOString::IsEmpty() const
  176.     { return m_nDataLength == 0; }
  177. _AFX_INLINE CMOString::operator lp_cc () const
  178.     { return (const char FAR *)m_pchData; }
  179. _AFX_INLINE int CMOString::Compare(const char FAR * psz) const
  180.     { return _fstrcmp(m_pchData, psz); }
  181. _AFX_INLINE int CMOString::CompareNoCase(const char FAR * psz) const
  182.     { return _fstricmp(m_pchData, psz); }
  183. //    _AFX_INLINE int CMOString::Collate(const char FAR * psz) const
  184. //        { return strcoll(m_pchData, psz); }        //    Need a model-independent version
  185. _AFX_INLINE void CMOString::MakeUpper()
  186.     { _fstrupr(m_pchData); }
  187. _AFX_INLINE void CMOString::MakeLower()
  188.     { _fstrlwr(m_pchData); }
  189. // Windows version in AFXWIN.H
  190. _AFX_INLINE void CMOString::MakeReverse()
  191.     { _fstrrev(m_pchData); }
  192. _AFX_INLINE char CMOString::GetAt(int nIndex) const
  193.     {
  194.         ASSERT(nIndex >= 0);
  195.         ASSERT(nIndex < m_nDataLength);
  196.  
  197.         return m_pchData[nIndex];
  198.     }
  199. _AFX_INLINE char CMOString::operator[](int nIndex) const
  200.     {
  201.         // same as GetAt
  202.  
  203.         ASSERT(nIndex >= 0);
  204.         ASSERT(nIndex < m_nDataLength);
  205.  
  206.         return m_pchData[nIndex];
  207.     }
  208. _AFX_INLINE void CMOString::SetAt(int nIndex, char ch)
  209.     {
  210.         ASSERT(nIndex >= 0);
  211.         ASSERT(nIndex < m_nDataLength);
  212.         ASSERT(ch != 0);
  213.  
  214.         m_pchData[nIndex] = ch;
  215.     }
  216. _AFX_INLINE BOOL AFXAPI operator==(const CMOString& s1, const CMOString& s2)
  217.     { return s1.Compare(s2) == 0; }
  218. _AFX_INLINE BOOL AFXAPI operator==(const CMOString& s1, const char FAR * s2)
  219.     { return s1.Compare(s2) == 0; }
  220. _AFX_INLINE BOOL AFXAPI operator==(const char FAR * s1, const CMOString& s2)
  221.     { return s2.Compare(s1) == 0; }
  222. _AFX_INLINE BOOL AFXAPI operator!=(const CMOString& s1, const CMOString& s2)
  223.     { return s1.Compare(s2) != 0; }
  224. _AFX_INLINE BOOL AFXAPI operator!=(const CMOString& s1, const char FAR * s2)
  225.     { return s1.Compare(s2) != 0; }
  226. _AFX_INLINE BOOL AFXAPI operator!=(const char FAR * s1, const CMOString& s2)
  227.     { return s2.Compare(s1) != 0; }
  228. _AFX_INLINE BOOL AFXAPI operator<(const CMOString& s1, const CMOString& s2)
  229.     { return s1.Compare(s2) < 0; }
  230. _AFX_INLINE BOOL AFXAPI operator<(const CMOString& s1, const char FAR * s2)
  231.     { return s1.Compare(s2) < 0; }
  232. _AFX_INLINE BOOL AFXAPI operator<(const char FAR * s1, const CMOString& s2)
  233.     { return s2.Compare(s1) > 0; }
  234. _AFX_INLINE BOOL AFXAPI operator>(const CMOString& s1, const CMOString& s2)
  235.     { return s1.Compare(s2) > 0; }
  236. _AFX_INLINE BOOL AFXAPI operator>(const CMOString& s1, const char FAR * s2)
  237.     { return s1.Compare(s2) > 0; }
  238. _AFX_INLINE BOOL AFXAPI operator>(const char FAR * s1, const CMOString& s2)
  239.     { return s2.Compare(s1) < 0; }
  240. _AFX_INLINE BOOL AFXAPI operator<=(const CMOString& s1, const CMOString& s2)
  241.     { return s1.Compare(s2) <= 0; }
  242. _AFX_INLINE BOOL AFXAPI operator<=(const CMOString& s1, const char FAR * s2)
  243.     { return s1.Compare(s2) <= 0; }
  244. _AFX_INLINE BOOL AFXAPI operator<=(const char FAR * s1, const CMOString& s2)
  245.     { return s2.Compare(s1) >= 0; }
  246. _AFX_INLINE BOOL AFXAPI operator>=(const CMOString& s1, const CMOString& s2)
  247.     { return s1.Compare(s2) >= 0; }
  248. _AFX_INLINE BOOL AFXAPI operator>=(const CMOString& s1, const char FAR * s2)
  249.     { return s1.Compare(s2) >= 0; }
  250. _AFX_INLINE BOOL AFXAPI operator>=(const char FAR * s1, const CMOString& s2)
  251.     { return s2.Compare(s1) <= 0; }
  252.  
  253.  
  254. inline ostream & operator << (ostream & os, 
  255.                               const CMOString & str)
  256.     {    //    for some reason, operator << does not like FAR pointers
  257.         //    so give it a near pointer.  Inefficient, but it works!
  258.     char buffer [256];
  259.     ASSERT (str.m_nDataLength < 256);
  260.     _fstrcpy (buffer, str.m_pchData);
  261.     return os << buffer;
  262.     }
  263.  
  264. #endif // _CMOSTR_H_
  265.